package com.agent.vpsserver.common.dto;

import com.github.pagehelper.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

@Data
public class PageBean<T> implements Serializable {
    private static final long serialVersionUID = 8656597559014685635L;
    @ApiModelProperty(name = "total", value = "总记录数", example = "")
    private long total;        //总记录数
    @ApiModelProperty(name = "list", value = "结果集")
    private List<T> list;    //结果集
    @ApiModelProperty(name = "pageNum", value = "页数", example = "")
    private int pageNum;    // 第几页
    @ApiModelProperty(name = "pageSize", value = "页码", example = "")
    private int pageSize;    // 每页记录数
    @ApiModelProperty(name = "pages", value = "总页数", example = "")
    private int pages;        // 总页数
    @ApiModelProperty(name = "size", value = "数据集的记录数", example = "")
    private int size;        // 当前页的数量 <= pageSize，该属性来自ArrayList的size属性

    /**
     * @param list
     */
    public PageBean(List<T> list) {
        if (list instanceof Page) {
            Page<T> page = (Page<T>) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.list = page;
            this.size = page.size();
        }
    }
    /**
     * 设置分页信息
     * @param list
     */
    public void setPageInfo(List<?> list) {
        if (list instanceof Page) {
            Page<?> page = (Page<?>) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.size = page.size();
            if (this.list == null) {
                this.list = Collections.emptyList();
            }
        }
    }
    public PageBean(){

    }

}